<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="#(package).business.mapper.#(className)Mapper">

	<resultMap type="#(package).model.#(className)" id="#(classname)Map">
		#for(column : tableColumns)
		<result property="#(column.attrname)" column="#(column.columnName)"/>
		#end
	</resultMap>

	<sql id="Base_Column_List" >
		#for(column : tableColumns)
			#(column.columnName)#if(!for.last),#end
		#end
	</sql>

	<select id="queryObject" resultType="#(package).model.#(className)" resultMap="#(classname)Map">
		select
		<include refid="Base_Column_List" />
		from #(tableName)
		where #(pk.columnName) = #{value}
	</select>

	<select id="queryList" resultType="#(package).model.#(className)" resultMap="#(classname)Map">
		select
		<include refid="Base_Column_List" />
		from #(tableName)
		<choose>
			<when test="sidx != null and sidx.trim() != ''">
				order by ${sidx} ${order}
			</when>
			<otherwise>
				order by #(pk.columnName) desc
			</otherwise>
		</choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>


	<select id="queryTotal" resultType="int">
		select count(*) from #(tableName)
	</select>

	<insert id="save" parameterType="#(package).model.#(className)"#if(pk.extra == 'auto_increment') useGeneratedKeys="true" keyProperty = "#(pk.attrname)" #end>
		insert into #(tableName)
		(
		#for(column : tableColumns)
		#if(column.columnName != pk.columnName || pk.extra != 'auto_increment')
		`#(column.columnName)`#if(!for.last), #end
		#end
		#end
		)
		values
		(
		#for(column : tableColumns)
		#if(column.columnName != pk.columnName || pk.extra != 'auto_increment')
		#{#(column.attrname)}#if(!for.last), #end
		#end
		#end
		)
	</insert>

	<update id="update" parameterType="#(package).model.#(className)">
		update #(tableName)
		<set>
			#for(column : tableColumns)
			#if(column.columnName != pk.attrname)
			<if test="#(column.attrname) != null">`#(column.columnName)` = #{#(column.attrname)},</if>
			#end
			#end
		</set>
		where #(pk.columnName) = #{#(pk.attrname)}
	</update>

	<delete id="delete">
		delete from #(tableName) where #(pk.columnName) = #{value}
	</delete>

	<delete id="deleteBatch">
		delete from #(tableName) where #(pk.columnName) in
		<foreach item="#(pk.columnName)" collection="array" open="(" separator="," close=")">
			#{#(pk.columnName)}
		</foreach>
	</delete>

</mapper>